package tuan.score;
 

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
public class ImgSizeScore extends NodeScore {
	String op;
	String attrName;
	int size;
	double score;

	public ImgSizeScore(String attrName, int size, String op, double score)
			throws Exception {
		this.score = score;
		this.op = op;
		this.size = size;
		this.attrName = attrName;
	}

	@Override
	protected double getScore(Node node) {
		double defaultScore = 0.0;

		if (node.hasAttributes()) {
			NamedNodeMap attrNodeList = node.getAttributes();
			if (attrNodeList.getNamedItem(attrName) != null) {
				try {
					int value = Integer.parseInt(attrNodeList.getNamedItem(attrName).getNodeValue());
					if (op.equals(">")) {
						return value > size ? score : defaultScore;
					}
					if (op.equals(">=")) {
						return value >= size ? score : defaultScore;
					}
					if (op.equals("<")) {
						return value < size ? score : defaultScore;
					}
					if (op.equals("<=")) {
						return value <= size ? score : defaultScore;
					}
					if (op.equals("!=")) {
						return value != size ? score : defaultScore;
					}
					return defaultScore;
				} catch (Exception e) {
					return defaultScore;
				}
			}
		}
		return defaultScore;
	}
}
